<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
use Hyperf\DbConnection\Db;

return new class extends Migration {

    /**
     * Run the migrations.
     */
    public function up(): void
    {

        $getTime = fn() => date('Y-m-d H:i:s', time());

        Schema::disableForeignKeyConstraints();

        /**
         * 项目配置表
         */
        Schema::create('configs', function (Blueprint $table) {
            $table->increments('id');
            $table->string('label', 30)->unique('label')->comment('配置标识');
            $table->string('name', 20)->comment('配置名称');
            $table->string('value', 1000)->comment('配置值');
            $table->unsignedTinyInteger('type')->default(1)->comment('配置类型');
            $table->string('desc', 1000)->comment('配置介绍');
            $table->datetimes();
            $table->comment('项目配置表');
        });

        // 插入基础数据
        Db::table('configs')->insert([
            ['label' => 'SERVER_DOMAIN', 'name' => '接口服务域名', 'value' => 'https://xxx.com/', 'desc' => '服务端对外提供的访问地址', 'created_at' => $getTime()],
            ['label' => 'FILE_DOMAIN', 'name' => '文件访问域名', 'value' => 'https://xxx.com/', 'desc' => '上传文件的对外访问域名', 'created_at' => $getTime()]
        ]);

        /**
         * 后台-管理员
         */
        Schema::create('rbac_admins', function (Blueprint $table) {
            $table->unsignedInteger('id', true);
            $table->char('auth_key', 18)->unique('auth_key')->nullable()->comment('登录凭证');
            $table->char('username', 30)->unique('username')->nullable()->comment('登录账户');
            $table->char('realname', 30)->nullable()->comment('人员姓名');
            $table->unsignedTinyInteger('state')->default(1)->comment('账户状态');
            $table->char('password', 60)->comment('登录密码');
            $table->datetimes();
            $table->string('avatar', 300)->nullable()->comment('头像链接');
            $table->comment('后台-管理员');
        });
        // 插入基础数据
        Db::table('rbac_admins')->insert([
            'username' => 'gaolei', 'realname' => '高雷', 'password' => '$2y$10$PGopCjUNlbEHes2.4VziC.JLtwhK7qqNlBVVA3ItgtCo2dWkCsvSG', 'created_at' => $getTime()
        ]);

        /**
         * 后台-权限
         */
        Schema::create('rbac_permissions', function (Blueprint $table) {
            $table->unsignedInteger('id', true);
            $table->unsignedInteger('parent_id')->default(0)->comment('父级权限');
            $table->string('name', 30)->unique('name')->comment('权限标识');
            $table->tinyInteger('order')->default(0)->comment('排序值');
            $table->string('method', 10)->nullable()->comment('请求方式');
            $table->string('path_uri', 60)->nullable()->comment('路由');
            $table->string('description', 100)->nullable()->comment('描述');
            $table->datetimes();
            $table->comment('后台-权限');
        });
        // 插入基础数据
        Db::table('rbac_permissions')->insert([
            ['id' => 1, 'parent_id' => 0, 'name' => '权限系统', 'order' => 99, 'method' => 'GET', 'path_uri' => '/rbac', 'created_at' => $getTime()],
            ['id' => 2, 'parent_id' => 1, 'name' => '账户管理', 'order' => 1, 'method' => 'GET', 'path_uri' => '/rbac/admin', 'created_at' => $getTime()],
            ['id' => 3, 'parent_id' => 1, 'name' => '权限管理', 'order' => 2, 'method' => 'GET', 'path_uri' => '/rbac/permission', 'created_at' => $getTime()],
            ['id' => 4, 'parent_id' => 1, 'name' => '角色管理', 'order' => 3, 'method' => 'GET', 'path_uri' => '/rbac/role', 'created_at' => $getTime()],
            ['id' => 5, 'parent_id' => 1, 'name' => '菜单管理', 'order' => 4, 'method' => 'GET', 'path_uri' => '/rbac/menu', 'created_at' => $getTime()],

            ['id' => 6, 'parent_id' => 2, 'name' => '账号列表', 'order' => 1, 'method' => 'GET', 'path_uri' => '/rbac/admin', 'created_at' => $getTime()],
            ['id' => 7, 'parent_id' => 2, 'name' => '账号添加', 'order' => 2, 'method' => 'POST', 'path_uri' => '/rbac/admin', 'created_at' => $getTime()],
            ['id' => 8, 'parent_id' => 2, 'name' => '账号修改', 'order' => 3, 'method' => 'PUT', 'path_uri' => '/rbac/admin/', 'created_at' => $getTime()],
            ['id' => 9, 'parent_id' => 2, 'name' => '账号删除', 'order' => 4, 'method' => 'DELETE', 'path_uri' => '/rbac/admin/', 'created_at' => $getTime()],
            ['id' => 10, 'parent_id' => 2, 'name' => '账号状态修改', 'order' => 5, 'method' => 'POST', 'path_uri' => '/rbac/admin/update-state', 'created_at' => $getTime()],

            ['id' => 11, 'parent_id' => 3, 'name' => '权限列表', 'order' => 1, 'method' => 'GET', 'path_uri' => '/rbac/permission', 'created_at' => $getTime()],
            ['id' => 12, 'parent_id' => 3, 'name' => '权限添加', 'order' => 2, 'method' => 'POST', 'path_uri' => '/rbac/permission', 'created_at' => $getTime()],
            ['id' => 13, 'parent_id' => 3, 'name' => '权限修改', 'order' => 3, 'method' => 'PUT', 'path_uri' => '/rbac/permission', 'created_at' => $getTime()],
            ['id' => 14, 'parent_id' => 3, 'name' => '权限删除', 'order' => 4, 'method' => 'DELETE', 'path_uri' => '/rbac/permission', 'created_at' => $getTime()],

            ['id' => 15, 'parent_id' => 4, 'name' => '角色列表', 'order' => 1, 'method' => 'GET', 'path_uri' => '/rbac/role', 'created_at' => $getTime()],
            ['id' => 16, 'parent_id' => 4, 'name' => '角色添加', 'order' => 2, 'method' => 'POST', 'path_uri' => '/rbac/role', 'created_at' => $getTime()],
            ['id' => 17, 'parent_id' => 4, 'name' => '角色修改', 'order' => 3, 'method' => 'PUT', 'path_uri' => '/rbac/role', 'created_at' => $getTime()],
            ['id' => 18, 'parent_id' => 4, 'name' => '角色删除', 'order' => 4, 'method' => 'DELETE', 'path_uri' => '/rbac/role', 'created_at' => $getTime()],

            ['id' => 19, 'parent_id' => 5, 'name' => '菜单列表', 'order' => 1, 'method' => 'GET', 'path_uri' => '/rbac/menu', 'created_at' => $getTime()],
            ['id' => 20, 'parent_id' => 5, 'name' => '菜单添加', 'order' => 2, 'method' => 'POST', 'path_uri' => '/rbac/menu', 'created_at' => $getTime()],
            ['id' => 21, 'parent_id' => 5, 'name' => '菜单修改', 'order' => 3, 'method' => 'PUT', 'path_uri' => '/rbac/menu', 'created_at' => $getTime()],
            ['id' => 22, 'parent_id' => 5, 'name' => '菜单删除', 'order' => 4, 'method' => 'DELETE', 'path_uri' => '/rbac/menu', 'created_at' => $getTime()],

            ['id' => 23, 'parent_id' => 0, 'name' => '项目配置', 'order' => 98, 'method' => 'GET', 'path_uri' => '/config', 'created_at' => $getTime()],
            ['id' => 24, 'parent_id' => 23, 'name' => '配置-列表', 'order' => 1, 'method' => 'GET', 'path_uri' => '/config', 'created_at' => $getTime()],
            ['id' => 25, 'parent_id' => 23, 'name' => '配置-添加', 'order' => 2, 'method' => 'POST', 'path_uri' => '/config', 'created_at' => $getTime()],
            ['id' => 26, 'parent_id' => 23, 'name' => '配置-修改', 'order' => 3, 'method' => 'PUT', 'path_uri' => '/config/', 'created_at' => $getTime()],
            ['id' => 27, 'parent_id' => 23, 'name' => '配置-删除', 'order' => 4, 'method' => 'DELETE', 'path_uri' => '/config/', 'created_at' => $getTime()],
            ['id' => 28, 'parent_id' => 23, 'name' => '配置-MAP', 'order' => 5, 'method' => 'GET', 'path_uri' => '/config/map', 'created_at' => $getTime()],
        ]);

        /**
         * 后台-菜单
         */
        Schema::create('rbac_menus', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('parent_id')->default(0)->comment('父级菜单');
            $table->unsignedInteger('permission_id')->default(0)->comment('对应权限');
            $table->string('name', 30)->comment('名称');
            $table->tinyInteger('order')->default(0)->comment('排序值');
            $table->unsignedTinyInteger('show')->default(1)->comment('是否显示');
            $table->string('icon', 80)->nullable()->comment('图标');
            $table->string('url', 80)->comment('页面路由');
            $table->string('additional', 300)->nullable()->comment('附加参数');
            $table->string('description', 100)->nullable()->comment('描述');
            $table->datetimes();
            $table->comment('后台-菜单');
        });
        // 插入基础数据
        Db::table('rbac_menus')->insert([
            ['id' => 1, 'parent_id' => 0, 'permission_id' => 0, 'name' => '系统管理', 'order' => 99, 'icon' => 'cog', 'url' => '/rbac', 'created_at' => $getTime()],
            ['id' => 2, 'parent_id' => 1, 'permission_id' => 2, 'name' => '账户管理', 'order' => 1, 'icon' => 'minus', 'url' => '/rbac/admin', 'created_at' => $getTime()],
            ['id' => 3, 'parent_id' => 1, 'permission_id' => 3, 'name' => '权限管理', 'order' => 2, 'icon' => 'minus', 'url' => '/rbac/permission', 'created_at' => $getTime()],
            ['id' => 4, 'parent_id' => 1, 'permission_id' => 4, 'name' => '角色管理', 'order' => 3, 'icon' => 'minus', 'url' => '/rbac/role', 'created_at' => $getTime()],
            ['id' => 5, 'parent_id' => 1, 'permission_id' => 5, 'name' => '菜单管理', 'order' => 4, 'icon' => 'minus', 'url' => '/rbac/menu', 'created_at' => $getTime()],
            ['id' => 6, 'parent_id' => 1, 'permission_id' => 23, 'name' => '项目配置', 'order' => 0, 'icon' => 'cogs', 'url' => '/configure', 'created_at' => $getTime()],
        ]);

        /**
         * 后台-角色
         */
        Schema::create('rbac_roles', function (Blueprint $table) {
            $table->unsignedInteger('id', true);
            $table->string('name', 30)->unique('name')->comment('角色标识');
            $table->string('display_name', 30)->comment('角色名称');
            $table->tinyInteger('state')->default(0)->comment('状态');
            $table->datetimes();
            $table->comment('后台-菜单');
        });
        // 插入基础数据
        Db::table('rbac_roles')->insert([
            ['id' => 1, 'name' => 'developer', 'display_name' => '开发者', 'created_at' => $getTime()],
            ['id' => 2, 'name' => 'operator', 'display_name' => '运营者', 'created_at' => $getTime()],
        ]);

        /**
         * 后台-角色_用户
         */
        Schema::create('rbac_role_admin', function (Blueprint $table) {
            $table->unsignedInteger('role_id')->index('role_id')->comment('角色ID');
            $table->unsignedInteger('uid')->comment('用户ID');
            $table->dateTime('created_at')->comment('添加时间');
            $table->primary(['role_id', 'uid']);
            $table->foreign('role_id', 'foreign-ra_r')->references('id')->on('rbac_roles')->onDelete('cascade')->onUpdate('restrict');
            $table->foreign('uid', 'foreign-ra_a')->references('id')->on('rbac_admins')->onDelete('cascade')->onUpdate('restrict');
            $table->comment('后台-角色_用户');
        });

        /**
         * 后台-角色_权限
         */
        Schema::create('rbac_role_permission', function (Blueprint $table) {
            $table->unsignedInteger('role_id')->comment('角色ID');
            $table->unsignedInteger('pms_id')->index('pms_id')->comment('权限ID');
            $table->dateTime('created_at')->comment('添加时间');
            $table->primary(['role_id', 'pms_id']);
            $table->foreign('role_id', 'foreign-rp_r')->references('id')->on('rbac_roles')->onDelete('cascade')->onUpdate('restrict');
            $table->foreign('pms_id', 'foreign_rp_p')->references('id')->on('rbac_permissions')->onDelete('cascade')->onUpdate('restrict');
            $table->comment('后台-角色_权限');
        });
        // 插入基础数据
        Db::table('rbac_role_permission')->insert([
            ['role_id' => 1, 'pms_id' => 1, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 2, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 3, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 4, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 5, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 6, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 7, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 8, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 9, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 10, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 11, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 12, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 13, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 14, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 15, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 16, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 17, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 18, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 19, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 20, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 21, 'created_at' => $getTime()],
            ['role_id' => 1, 'pms_id' => 22, 'created_at' => $getTime()],
        ]);

        Schema::enableForeignKeyConstraints();
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::disableForeignKeyConstraints();

        Schema::dropIfExists('rbac_role_permission');
        Schema::dropIfExists('rbac_role_admin');
        Schema::dropIfExists('rbac_roles');
        Schema::dropIfExists('rbac_menus');
        Schema::dropIfExists('rbac_permissions');
        Schema::dropIfExists('rbac_admins');
        Schema::dropIfExists('configs');

        Schema::enableForeignKeyConstraints();
    }
};
